﻿@using NewLife;
@using System.ComponentModel;
@using System.Reflection;
@using NewLife.Reflection;
@using NewLife.Serialization;
@{
    //Layout = NewLife.Cube.Setting.Current.Layout;

    var obj = (Object)Model;
    var list = ViewBag.Fields as IList<DataField>
    ;
    var dic = list
    .GroupBy(e => e.Category + "")
    .ToDictionary(e => e.Key, e => e.ToList());

    var modelDict = new Dictionary<string, object>();



    foreach (var item in list)
    {
        modelDict[item.Name] = obj.GetValue(item.Name);
    }
}


<div>
    <el-link href="/Admin/Core">基本设置</el-link>
    <el-link href="/Admin/Sys">系统设置</el-link>
    <el-link href="/Admin/XCode">数据中间件</el-link>
    <el-link href="/Admin/Cube">魔方设置</el-link>
    <el-link href="/Admin/OAuthConfig">OAuth设置</el-link>


</div>
<div>


    <el-form v-bind:model="modelForm" size="mini" ref="modelForm" label-width="100px">

        @foreach (var item in dic)
        {
            if (dic.Count > 1)
            {
                <div class="form-group col-sm-12 col-md-12 col-xd-12">
                    <label class="control-label col-xs-2 col-md-3 no-padding-left">
                        <h2>@item.Key</h2>
                    </label>
                </div>
            }
            foreach (var pi in item.Value)
            {
                var name = pi.Name;
                var val = obj.GetValue(name);
                var dis = pi.DisplayName;
                var des = pi.Description;
                var dataSource = pi.DataSource;


                <el-form-item label="@dis" prop="@name">

                    @if (dataSource != null)
                    {
                        @await Html.PartialAsync("_Form_DropDownList", new ListBoxModel(name, dataSource(item.Value), val))
                    }
                    else
                    {
                        @await Html.PartialAsync("_Form_Editor", new ItemModel(name, val, pi.Type))
                    }

                    @des
                </el-form-item>
            }
        }

        @if (this.Has(PermissionFlags.Update))
        {
            <el-form-item>
                <el-button v-on:click="onCloseModelFrom('modelForm')">取 消</el-button>
                <el-button type="primary" v-on:click="onSaveSubmit('modelForm')">保 存</el-button>
            </el-form-item>

        }
    </el-form>

</div>



@section scripts{

    <script>
        //Vue2.0 桌面端组件库: https://element.eleme.cn/#/zh-CN/component/cascader

        var main = new Vue({
            el: '#app',
            data: {
                modelForm: @Html.Raw(modelDict.ToJson()),
                //选项卡选中项id
                tabIndex: 10000
            },
            //页面初始化
            created() {

            },
            methods: {
                //编辑表单提交
                onSaveSubmit(formName) {
                    let self = this;
                    var formData = new FormData();
                    for (const key in this.modelForm) {
                        if (this.modelForm[key] != null) {
                            formData.append(key, this.modelForm[key]);
                        }
                    }

                    axios.post('@HttpContext.Current.Request.Path.Value/Update', formData)
                        .then(function (response) {

                            self.$message.success('操作成功');

                        }).catch((error) => {
                            console.log(error);
                            self.$message.error('操作失败');

                        });

                },
            },
        });

    </script>

}